// pages/editaddress/index.js
import { areaList } from '@vant/area-data';
import { addAddress, editAdd, getItemAdd } from '../../../utils/api';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    areaList,
    showAddress: false,
    addressInfo: '',//页面地址回显
    name: '',
    phone: '',
    isDefault: false,
    province: '',//省
    city: '',//市
    area: '',//区
    address: '',//详细地址
    type: '', //edit add
    addId: '',
    inputErr: '',
    nameErr: '',
    hasSpace: '',
    isPhone: ''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.id) {
      this.setData({
        title: '修改地址',
        type: 'edit',
        addId: options.id
      })
      //获取地址回显
      let params = {
        addid: options.id
      }
      getItemAdd(params).then((res) => {

        if (res.errcode == 0) {
          let resdata = res.address
          this.setData({
            addressInfo: `${resdata.province}${resdata.city}${resdata.area}`,//页面地址回显
            name: resdata.name,
            phone: resdata.phone,
            isDefault: resdata.is_default == 0 ? false : true,
            address: resdata.address,//详细地址
            province: resdata.province,//省
            city: resdata.city,//市
            area: resdata.area,//区
            hasSpace: false,
            isPhone: true
          })
        }
      })
    } else {
      this.setData({
        title: '添加地址',
        type: 'add'
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  onBack() {
    wx.navigateBack({
      delta: 1
    })
  },

  //选择地址
  chooseAdd() {
    this.setData({
      showAddress: true,
    })

  },
  //改变地址
  changeAdds(e) {
    this.setData({
      addressInfo: `${e.detail.values[0].name}${e.detail.values[1].name}${e.detail.values[2].name}`
    })
  },

  //关闭
  closeAdd() {
    this.setData({
      showAddress: false
    })
  },

  //确定
  confirmAdd(e) {
    if (!this.data.addressInfo) {
      this.setData({
        addressInfo: '北京市北京市东城区'
      })
    }
    this.setData({
      province: e.detail.values[0].name,
      city: e.detail.values[1].name,
      area: e.detail.values[2].name,
      showAddress: false
    })
  },

  //name
  handleName(e) {
    var reg = /\s/;
    this.setData({
      name: e.detail
    })
    var hasSpace = reg.test(this.data.name);
    this.setData({
      hasSpace: hasSpace
    })

    if (hasSpace) {
      this.setData({
        nameErr: '不允许输入空格'
      })
    } else {
      this.setData({
        nameErr: ''
      })
    }
  },
  //addresss
  handleAddress(e) {
    this.setData({
      address: e.detail.value
    })

  },

  //phone
  handlePhone(e) {
    var reg = /^1[3-9]\d{9}$/;
    this.setData({
      phone: e.detail.value
    })
    var isPhone = reg.test(this.data.phone)
    this.setData({
      isPhone: isPhone
    })
    if (isPhone) {
      this.setData({
        inputErr: ''
      })
    } else {
      this.setData({
        inputErr: '手机号格式错误'
      })
    }


  },

  //设置默认地址
  checkDefaule() {
    this.setData({
      isDefault: !this.data.isDefault
    })
  },


  //保存地址
  onAddress() {
    //判断填写完整
    if (this.data.name == '' || this.data.phone == '' || this.data.address == '' || this.data.addressInfo == '') {
      wx.showToast({
        title: '请将收货信息填写完整',
        icon: 'none',
      });

    }

    else {
      //判断格式
      if (this.data.hasSpace || !this.data.isPhone) {

        wx.showToast({
          title: '请填写正确格式',
          icon: 'none',
        });
      } else {
        if (this.data.type == 'add') {
          let addParams = {
            name: this.data.name,
            phone: this.data.phone,
            province: this.data.province,
            city: this.data.city,
            area: this.data.area,
            address: this.data.address,
            is_default: this.data.isDefault == false ? '0' : '1'
          }

          addAddress(addParams).then((res) => {
            if (res.errcode == 0) {
              wx.showToast({
                title: '添加成功',
                icon: 'success',
                success: (result) => {
                  wx.navigateBack({
                    delta: 1
                  });
                },

              });

            } else {
              wx.showToast({
                title: res.errmsg,
                icon: 'none',
              });
            }
          })
        } else {
          let editParams = {
            addid: this.data.addId,
            name: this.data.name,
            phone: this.data.phone,
            province: this.data.province,
            city: this.data.city,
            area: this.data.area,
            address: this.data.address,
            is_default: this.data.isDefault == false ? '0' : '1'
          }
          editAdd(editParams).then((res) => {
            if (res.errcode == 0) {
              wx.showToast({
                title: '修改成功',
                icon: 'success',
                success: (result) => {
                  wx.navigateBack({
                    delta: 1
                  });
                },

              });
            } else {
              wx.showToast({
                title: res.errmsg,
                icon: 'none',
              });

            }
          })
        }

      }

    }
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },



})